package com.yuan.springcloud.product.bean;


import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * web返回模型包装工具类
 * @author YouYuan
 * @version 1.0
 */
public class Result {

    public static final String KEY_STATE = "state";
    public static final String KEY_MESSAGE = "message";
    public static final String KEY_DATA = "data";
    public static final String KEY_TOTAL = "total";
    public static final String MSG_SUCCESS = "success";
    public static final String MSG_FAIL = "fail";
    public static final int STATE_SUCCESS = 0;
    public static final int STATE_FAIL = 1;

    public static <T> Map success() {
        Map map = new HashMap(3);
        map.put(KEY_STATE, STATE_SUCCESS);
        map.put(KEY_MESSAGE, MSG_SUCCESS);
        return map;
    }

    public static <T> Map success(T data) {
        Map map = new HashMap(5);
        map.put(KEY_DATA, data);
        map.put(KEY_STATE, STATE_SUCCESS);
        map.put(KEY_MESSAGE, MSG_SUCCESS);
        return map;
    }

    public static <T> Map success(Collection data, Number total) {
        Map map = new HashMap(6);
        map.put(KEY_DATA, data);
        map.put(KEY_STATE, STATE_SUCCESS);
        map.put(KEY_TOTAL, total);
        map.put(KEY_MESSAGE, MSG_SUCCESS);
        return map;
    }

    public static <T> Map success(Collection data, Number total, String dataKey) {
        Map map = new HashMap(5);
        map.put(dataKey, data);
        map.put(KEY_STATE, STATE_SUCCESS);
        map.put(KEY_TOTAL, total);
        map.put(KEY_MESSAGE, MSG_SUCCESS);
        return map;
    }

    public static Map fail(String msg) {
        Map map = new HashMap(5);
        map.put(KEY_DATA, null);
        map.put(KEY_STATE, STATE_FAIL);
        map.put(KEY_MESSAGE, msg == null ? MSG_FAIL : msg);
        return map;
    }

    public static <T> Map fail(T data, String msg) {
        Map map = new HashMap(5);
        map.put(KEY_DATA, data);
        map.put(KEY_STATE, STATE_FAIL);
        map.put(KEY_MESSAGE, msg == null ? MSG_FAIL : msg);
        return map;
    }

    public static <T> Map render(T data, Object state, String msg) {
        Map map = new HashMap(5);
        map.put(KEY_DATA, data);
        map.put(KEY_STATE, state);
        map.put(KEY_MESSAGE, msg);
        return map;
    }
}
